<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/admin/list' }">
                基础管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/admin/list' }"
                >账号列表</el-breadcrumb-item
            >
            <el-breadcrumb-item>添加管理账号</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="添加管理员账号">
            </el-page-header>
            <el-form
                :model="addForm"
                :rules="addFormRules"
                ref="addFormRef"
                label-width="80px"
                hide-required-asterisk
            >
                <el-form-item label="账号" prop="account">
                    <el-input v-model="addForm.account"> </el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="addForm.password">
                    </el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name"> </el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"> </el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="addForm.mobile"> </el-input>
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="addForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="subAdd"
                        >立即创建</el-button
                    >
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import validator from '@/validator/index.js'
export default {
    data () {
        return {
            addForm: {
                account: '',
                password: '',
                name: '',
                email: '',
                mobile: '',
                status: 1
            },
            // 添加表单验证规则对象
            addFormRules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 2, max: 20, message: '账号长度在2~20位之间', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 34, message: '账号长度在6~34位之间', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 6, message: '账号长度在2~6位之间', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { min: 6, max: 34, message: '账号长度在6~34位之间', trigger: 'blur' },
                    { validator: validator.checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { validator: validator.checkMobile, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        goBack () {
            this.$router.back()
        },
        subAdd () {
            this.$refs.addFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveAdminAdd(this.addForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.addFormRef.resetFields()
            this.addForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
